//
//  LPNetWroking.swift
//  TLenPoint
//
//  Created by Docker on 2018/3/21.
//  Copyright © 2018年 Docker. All rights reserved.
//

import UIKit
import AFNetworking
import YYModel
enum RequestType{
    //get请求
    case Get
    //Post请求
    case Post
}
class LPNetWroking: AFHTTPSessionManager {

    //全局单列
    static let lpsessionManager :LPNetWroking = {
        let netManager = LPNetWroking()
        netManager.responseSerializer = AFJSONResponseSerializer()
        netManager.responseSerializer.acceptableContentTypes?.insert("text/html")
        netManager.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return netManager
    }()
    
    //网络请求的通用方法
    func requestData(requestType:RequestType,urlString:String,params:Any?,callBack:@escaping (Any?,Error?)->()){
        //get请求
         CommanAction().LPLogLine(message: params)
        if requestType == .Get{
            get(urlString, parameters: params, progress: nil, success: { (_, responseObject) in
                
                callBack(responseObject,nil)
            }, failure: { (_, error) in
                callBack(nil,error)
            })
        }else{
        //post请求
            post(urlString, parameters: params, progress: nil, success: { (_, responseObject) in
                callBack(responseObject,nil)
            }, failure: { (_, error) in
                print(error)
                callBack(nil,error)
            })
        }
    }
}
